package org.mozhu.mboot.core.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * 基础控制器
 */
public abstract class BaseController {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	protected static final String OPERATION_VIEW = "op.view";
	protected static final String OPERATION_ADD = "op.add";
	protected static final String OPERATION_EDIT = "op.edit";
	protected static final String OPERATION_DELETE = "op.delete";

	protected static final String OPERATION_SUCCESS = "op.success";
	protected static final String CREATE_SUCCESS = "op.create.success";
	protected static final String UPDATE_SUCCESS = "op.update.success";
	protected static final String DELETE_SUCCESS = "op.delete.success";

	protected static final String OPERATION_FAIL = "op.fail";
	protected static final String CREATE_FAIL = "op.create.fail";
	protected static final String UPDATE_FAIL = "op.update.fail";
	protected static final String DELETE_FAIL = "op.delete.fail";
	
	private String viewPrefix;

	protected BaseController() {
		setViewPrefix(defaultViewPrefix());
	}

	/**
	 * 设置通用数据
	 * 
	 * @param model
	 */
	protected void setCommonData(Model model) {
		model.addAttribute("baseUrl", getViewPrefix());
	}

	public void setViewPrefix(String viewPrefix) {
		if (viewPrefix.startsWith("/")) {
			viewPrefix = viewPrefix.substring(1);
		}
		this.viewPrefix = viewPrefix;
	}

	public String getViewPrefix() {
		return viewPrefix;
	}

	/**
	 * @param url
	 *            为null 将重定向到默认getViewPrefix()
	 * @return
	 */
	protected String redirect(String url) {
		if (StringUtils.isEmpty(url)) {
			url = "/" + getViewPrefix();
		}
		if (!url.startsWith("/") && !url.startsWith("http")) {
			url = "/" + getViewPrefix() + "/" + url;
		}
		return "redirect:" + url;
	}

	public String redirect(String url, Map<String, String> params) {
		return redirect(url + "?" + toParamString(params));
	}

	public String toParamString(Map<String, String> params) {
		if (params == null || params.isEmpty()) {
			return "";
		}
		String paramString = "";
		for (Map.Entry<String, String> entry : params.entrySet()) {
			paramString += "&" + entry.getKey() + "=" + entry.getValue();
		}
		return paramString.substring(1);
	}

	/**
	 * 获取视图名称：即prefixViewName + "/" + suffixName
	 * 
	 * @return
	 */
	public String viewName(String suffixName) {
		if (!suffixName.startsWith("/")) {
			suffixName = "/" + suffixName;
		}
		return getViewPrefix() + suffixName;
	}
	
	/**
	 * 当前模块 视图的前缀 默认 <br/>
	 * 1、获取当前类头上的@RequestMapping中的value作为前缀 <br/>
	 * 2、如果没有就使用当前模型小写的简单类名
	 */
	protected String defaultViewPrefix() {
		String currentViewPrefix = "";
		RequestMapping requestMapping = AnnotationUtils.findAnnotation(getClass(), RequestMapping.class);
		if (requestMapping != null && requestMapping.value().length > 0) {
			currentViewPrefix = requestMapping.value()[0];
		}
		return currentViewPrefix;
	}
}
